package com.zeng.plugin.util;

import com.zeng.plugin.entity.RowEntity;
import com.zeng.plugin.entity.TableEntity;
import lombok.Data;
import org.dom4j.Attribute;
import org.dom4j.DocumentException;
import org.dom4j.Element;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.Stream;

@Data
public class ParseXmlUtil {

    private List<TableEntity> tableEntities = new ArrayList<TableEntity>();

    public ParseXmlUtil() {

    }

    public String humpStringHandle(String nohump) {
        if (nohump == null) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder(nohump);
        stringBuilder.setCharAt(0, nohump.toUpperCase().charAt(0));
        int position = stringBuilder.indexOf("_");

        while (position >= 0) {
            stringBuilder.delete(position,position+1);
            char ch = stringBuilder.charAt(position);
            if (ch > 90) {
                ch = (char) (ch - 32);
            }
            stringBuilder.setCharAt(position, ch);
            position = stringBuilder.indexOf("_");

        }

        return stringBuilder.toString();

    }

    public void parse(String filePath) throws DocumentException, ClassNotFoundException {
        String author = "";
        Dom4jUtil dom4jUtil = new Dom4jUtil(filePath);
        Element root = dom4jUtil.getRoot();
        Attribute authorAttr = root.attribute("author");
        if (authorAttr != null) {
            author = authorAttr.getValue();
        }
        List<Element> tables = dom4jUtil.getChildren(root);
        if (tables.isEmpty()) {
            return;
        }
//        遍历所有的表
        for (Element table : tables) {
            TableEntity tableEntity = new TableEntity();
//           设置属性
            tableEntity.setAuthor(author);
            Attribute realTableName = table.attribute("name");
            if (realTableName == null) {
                throw new RuntimeException("table name is not null");
            }
            tableEntity.setRealTableName(realTableName.getValue());
            tableEntity.setClassName(this.humpStringHandle(tableEntity.getRealTableName()));

            Attribute desc = table.attribute("desc");
            if (desc != null) {
                tableEntity.setDesc(desc.getValue());
            }

            List<Element> rowTypes = dom4jUtil.getChildren(table);
            List<RowEntity> rowEntities = new ArrayList<RowEntity>();
//            遍历类型标签 如 Integer String
            for (Element rowType : rowTypes) {
                String type = rowType.getQualifiedName();
                List<Element> rowName = dom4jUtil.getChildren(rowType);
//                遍历属性标签
                for (Element element : rowName) {
                    RowEntity rowEntity = new RowEntity();
//                    设置type 类型
                    rowEntity.setType(Class.forName(type));
//                    设置length 有就设置没有就 null
                    Attribute length = element.attribute("length");
                    if (length != null) {
                        rowEntity.setLength(Integer.valueOf(length.getValue()));
                    }
//                    设置名字
                    rowEntity.setName(element.attribute("value").getValue());
//                    设置描述
                    Attribute attributeDesc = element.attribute("desc");
                    //添加like字段
                    Attribute like = element.attribute("like");
                    if (like != null) {
                        tableEntity.getLikeProperties().add(rowEntity.getName());
                    }
                    if (attributeDesc == null) {
                        rowEntity.setDesc("");
                    }else{
                        rowEntity.setDesc(attributeDesc.getValue());
                    }

                    if (attributeDesc != null) {
                        rowEntity.setDesc(attributeDesc.getValue());
                    }
                    rowEntities.add(rowEntity);
                }
            }
            tableEntity.setRows(rowEntities);
            tableEntities.add(tableEntity);
        }

    }

//    public static void main(String[] args) throws ClassNotFoundException, DocumentException, IOException, TemplateException {
//
//        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
//        File file = new File(contextClassLoader.getResource("./").getPath());
//        String absolutePath = file.getAbsolutePath();
//        System.out.println(absolutePath);
//        ParseXmlUtil parseXmlUtil = new ParseXmlUtil();
//        parseXmlUtil.parse(absolutePath+"/tables.xml");
//        List<TableEntity> tableEntities = parseXmlUtil.getTableEntities();
//        Map<String, Object> data = new HashMap<>();
//        data.put("table",tableEntities.get(0));
//        FreeMarkUtil freeMarkUtil = new FreeMarkUtil();
//        freeMarkUtil.parse("template.ftl","test.java",data);
//        System.out.println(tableEntities);
//
//    }

    public static void main(String[] args) {
//        生成流
//  name = zhangsan
//  age = 13
//        salary = 4000
        ParseXmlUtil parseXmlUtil = new ParseXmlUtil();
        String user_user_user = parseXmlUtil.humpStringHandle("user_user_user");
        System.out.println(user_user_user);
        StringBuilder builder = new StringBuilder("user_user_user");
        while (true) {
            int i1 = builder.indexOf("_");
            builder.delete(i1, i1 + 1);
            System.out.println(builder.toString());

            if (!(i1 >= 0)) {
                break;
            }

        }



        /*数组创建流*/
        int[] ints = {1, 2, 3, 4, 5, 6};
        IntStream stream = Arrays.stream(ints);

        /*对象创建流*/
        Map<String, String> map = new HashMap();
        map.put("name", "zhangsan");
        Stream<Object> objectStream = Stream.of(1, "2", map);

        /*
         * 流的中间操作
         * */
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Map<String, Object> tempmap = new HashMap<>();
            tempmap.put("name", "zhangsan" + i);
            tempmap.put("age", i * 10);
            tempmap.put("salart", i * 4000.0);
            list.add(tempmap);
        }
        /*筛选与切片*/
        /*过滤*/
        Stream<Map<String, Object>> mapStream = list.stream().filter(e -> e.get("name").equals("zhangsan1"));
        System.out.println(mapStream.collect(Collectors.toList()));
        //[{salart=4000.0, name=zhangsan1, age=10}]
        /*去重*/
        Stream<Map<String, Object>> distinct = list.stream().distinct();
        System.out.println(distinct.collect(Collectors.toList()));
//       // 利用hash 和equeal 去重
        //[{salart=0.0, name=zhangsan0, age=0},
        // {salart=4000.0, name=zhangsan1, age=10},
        // {salart=8000.0, name=zhangsan2, age=20},
        // {salart=12000.0, name=zhangsan3, age=30},
        // {salart=16000.0, name=zhangsan4, age=40}]
        /*限制*/
        Stream<Map<String, Object>> limit = list.stream().limit(2);
        System.out.println(limit.collect(Collectors.toList()));
        /*跳过元素 与limit 互补*/
        Stream<Map<String, Object>> skip = list.stream().skip(2);
        System.out.println(skip.collect(Collectors.toList()));

        /*映射*/
        Stream<Object> name = list.stream().map(e -> e.get("name"));
        System.out.println(name.collect(Collectors.toList()));//[zhangsan0, zhangsan1, zhangsan2, zhangsan3, zhangsan4]
        DoubleStream doubleStream = list.stream().mapToDouble(e -> (Double) e.get("salary"));
        String[] strings = new String[]{"hello","world"};
        Stream<String> stream1 = Arrays.stream(strings);
        Stream<String[]> stream2 = stream1.map(e -> e.split(""));
        /*返回值为流 可以将返回的流合并*/
        Stream<String> stringStream = stream2.flatMap(e -> Arrays.stream(e));
        List<String> collect = stringStream.collect(Collectors.toList());
        System.out.println(collect);//[h, e, l, l, o, w, o, r, l, d]
        /*
        *
        * */

    }

}
